2 '***************************** Module Header ******************************\
3 '* Module Name: LoginPage.aspx.vb
4 '* Project: AzureBingMaps
5 '* Copyright (c) Microsoft Corporation.
7 '* The callback handler. Configure both ACS and Messenger Connect
8 '* to redirect to this page after the user signs in.
10 '* This source is subject to the Microsoft Public License.
11 '* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
12 '* All other rights reserved.
14 '* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
15 '* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
16 '* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
17 '\**************************************************************************
21 Imports System
.ServiceModel
.Syndication
22 Imports System
.Threading
25 Imports Microsoft
.IdentityModel
.Claims
27 Partial
Public Class FederationCallbackHandler
28 Inherits System
.Web
.UI
.Page
29 Protected
Sub Page_Load(ByVal sender
As Object, ByVal e
As EventArgs
)
30 ' Obtain return page from session, and redirect to the page later.
31 Dim returnPage
As String = "HtmlClient.aspx"
32 If Session("ReturnPage") IsNot
Nothing Then
33 returnPage
= DirectCast(Session("ReturnPage"), String)
36 ' Parse wl_internalState cookie,
37 ' and extract information for Windows Live Messenger Connect Profile API.
38 ' wl_internalState could be null if the user hasn't tried to login using Live ID.
39 If Response
.Cookies("wl_internalState") IsNot
Nothing Then
40 Dim accessToken
As String = Me.ExtractWindowsLiveInternalState("wl_accessToken")
41 Dim cid
As String = Me.ExtractWindowsLiveInternalState("wl_cid")
42 Dim uri
As String = "http://apis.live.net/V4.1/cid-" & cid
& "/Profiles"
44 ' wl_internalState could be invalid if LiveID login failed.
45 If Not String.IsNullOrEmpty(accessToken
) AndAlso
Not String.IsNullOrEmpty(cid
) Then
47 ' Make a request to profile API.
48 Dim request
As HttpWebRequest
= DirectCast(HttpWebRequest
.Create(uri
), HttpWebRequest
)
49 request
.Headers("Authorization") = accessToken
50 Dim response
As HttpWebResponse
= DirectCast(request
.GetResponse(), HttpWebResponse
)
51 If response
.StatusCode
= HttpStatusCode
.OK
Then
53 ' Use WCF Syndication API to parse the response.
54 Dim xmlReader
As XmlReader
= XmlReader
.Create(response
.GetResponseStream())
55 Dim feed
As SyndicationFeed
= SyndicationFeed
.Load(xmlReader
)
56 Dim entry
= feed
.Items
.FirstOrDefault()
57 If entry IsNot
Nothing Then
58 Dim content
= TryCast(entry
.Content
, XmlSyndicationContent
)
59 If content IsNot
Nothing Then
61 ' WindowsLiveProfile is a class
62 ' corresponding to the profile API's response.
63 Dim profile
= content
.ReadContent(Of WindowsLiveProfile
)()
64 Dim liveID
= profile
.Emails
.Where(Function(m
) String.Equals(m
.Type, "WindowsLiveID")).FirstOrDefault()
66 ' If profile API succeeds,
67 ' we'll be able to obtain the user's LiveID.
68 ' The LiveID will be the user's identity.
69 ' We store user identity in session.
70 If liveID IsNot
Nothing Then
71 Session("User") = liveID
.Address
80 ' The following code deals with ACS via WIF.
81 Dim principal
= TryCast(Thread
.CurrentPrincipal
, IClaimsPrincipal
)
82 If principal IsNot
Nothing AndAlso principal
.Identities
.Count
> 0 Then
83 Dim identity
= principal
.Identities(0)
85 ' Query for email claim.
86 Dim query
= From c
In identity
.Claims Where c
.ClaimType
= ClaimTypes
.Email
Select c
87 Dim emailClaim
= query
.FirstOrDefault()
88 If emailClaim IsNot
Nothing Then
90 ' Store user identity in session.
91 Session("User") = emailClaim
.Value
94 ' Redirect user to the return page.
95 Response
.Redirect(returnPage
)
99 ''' Extract information for Windows Live Messenger Connect Profile API
100 ''' from wl_internalState cookie.
101 ''' The cookie contains a bunch of information
102 ''' such as cid and access token.
104 ''' <param name="key">Which data to extract.</param>
105 ''' <returns>The value of the data.</returns>
106 Private Function ExtractWindowsLiveInternalState(ByVal key
As String) As String
107 Dim result
As String = Request
.Cookies("wl_internalState").Value
109 result
= HttpUtility
.UrlDecode(result
)
110 result
= result
.Substring(result
.IndexOf(key
))
111 result
= result
.Substring(key
.Length
+ 3, result
.IndexOf(","c
) - key
.Length
- 4)
113 ' wl_internalState could be invalid if LiveID login failed.
114 ' In this case, we return null.